using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Interactor_WaterFlow_Ctrl : Unit_Interactor_Ctrl
{
    public Transform trigger_Case;
    public HashSet<Unit_Charactor_Ctrl> innerCharactor=new HashSet<Unit_Charactor_Ctrl>();
    public Transform particle_Transform;
    public float directionDistance = 2;
    public float velocityScale = 1;
    public Vector3 currentVelocity;
    private void Start()
    {
        trigger_Case.transform.localScale = new Vector3(1, 0.8f, 1);
        particle_Transform.transform.eulerAngles = this.transform.eulerAngles;
    }
    private void Update()
    {
        for (int i = 0; i < innerUnits.Count; i++)
        {
            Unit_Base unit_Ctrl = innerUnits[i];
            switch (unit_Ctrl.unitType)
            {
                case UnitType.Charactor:
                    Unit_Charactor_Ctrl unit_Charactor_Ctrl = (Unit_Charactor_Ctrl)unit_Ctrl;
                   
                    //velocity.x = Mathf.Abs(unit_Charactor_Ctrl.rigibody.velocity.x) >= Mathf.Abs(velocity.x) ? unit_Charactor_Ctrl.rigibody.velocity.x : velocity.x;
                    //velocity.y = Mathf.Max(velocity.y, unit_Charactor_Ctrl.rigibody.velocity.y);
                    //unit_Charactor_Ctrl.rigibody.velocity = velocity;
                    
      
                    //unit_Charactor_Ctrl.rigibody.velocity = new Vector3(unit_Charactor_Ctrl.rigibody.velocity.x, velocity.y);
                    break;
            }
        }

    }

    public override void ReceiveLight(Vector3 sourcePos, Vector3 sourceDirection)
    {
        base.receiveLightEvent?.Invoke(sourcePos, sourceDirection);
    }
    protected override void OnUnitTriggerEnter(Unit_Base unit_Base)
    {
        base.OnUnitTriggerEnter(unit_Base);
        switch (unit_Base.unitType)
        {
            case UnitType.Charactor:
                Unit_Charactor_Ctrl unit_Charactor_Ctrl = (Unit_Charactor_Ctrl)unit_Base;
                //unit_Charactor_Ctrl.moveLocks.Add(this.GetInstanceID().ToString());
                //unit_Charactor_Ctrl.jumpValue = Mathf.Max(1, unit_Charactor_Ctrl.jumpValue);
               if (innerCharactor.Add(unit_Charactor_Ctrl))
                {
                    currentVelocity = ((this.transform.position + this.transform.up * directionDistance) - unit_Charactor_Ctrl.transform.position).normalized* velocityScale;
                    trigger_Case.transform.localScale = new Vector3(1, 1.5f, 1);
                    unit_Charactor_Ctrl.jumpValue = 1;
                    unit_Charactor_Ctrl.rigibody.gravityScale = 0;
                    unit_Charactor_Ctrl.otherVelocityLimit = velocityScale;
                    unit_Charactor_Ctrl._otherVelocity += currentVelocity;
                    //unit_Charactor_Ctrl._otherVelocity = Vector3.ClampMagnitude(unit_Charactor_Ctrl._otherVelocity, velocityScale);
                    unit_Charactor_Ctrl.AddNoGravityKey(this.GetInstanceID().ToString());
                }
                break;
        }
    }
    protected override void OnUnitTriggerExit(Unit_Base unit_Base)
    {
        base.OnUnitTriggerExit(unit_Base);
        switch (unit_Base.unitType)
        {
            case UnitType.Charactor:
                Unit_Charactor_Ctrl unit_Charactor_Ctrl = (Unit_Charactor_Ctrl)unit_Base;
                if (innerCharactor.Remove(unit_Charactor_Ctrl))
                {
                    unit_Charactor_Ctrl._otherVelocity -= currentVelocity;
                    unit_Charactor_Ctrl.RemoveNoGravityKey(this.GetInstanceID().ToString());
                    unit_Charactor_Ctrl.rigibody.gravityScale = 0.32f;
                }
                if (innerCharactor.Count <= 0)
                {
                    trigger_Case.transform.localScale = new Vector3(1, 1, 1);
                }
                break;
        }
    }
}
